﻿using DongFangGuoKai.WMSPlatform.Commn.ExtendModule;
using Microsoft.AspNetCore.Mvc.Filters;

namespace DongFangGuoKai.WMSPlatform.WebApi.Utility.Filters
{
    public class CtmActionFilterAttribute : Attribute, IAsyncActionFilter
    {
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var data = $"Controller:{context.ActionDescriptor} ，时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},Json:{context.ActionArguments?.ToJson()} "; 

            string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log",$"{DateTime.Now.ToString("yyyy-MM-dd")}.txt"); // 指定要保存的日志文件路径
            if (!File.Exists(path))
            {
                var  file=  File.Create(path);
                file.Close();   
            }
       
            try
            {
                File.AppendAllText(path, data);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"发生错误：{ex.Message}");
            }
            await next.Invoke();
        }
    }
}
